package kku.cs.fgl.actor;

import java.io.IOException;
import java.io.InputStream;

import kku.cs.fgl.Actor;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class ImageActor extends Actor {
	private Image img[] = null;

	private String imgFile[] = null;

	private Color tranColor = null;

	private boolean checkPixel;

	private int delay = 100;

	private int curframe = 0;

	private boolean active = true;

	public ImageActor(String... imageFile) {
		super();
		this.imgFile = imageFile;
		this.setSize(0, 0);
		try {
			loadImage(tranColor, imgFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ImageActor(Color tranColor, String... imageFile) {
		super();
		this.imgFile = imageFile;
		this.tranColor = tranColor;
		this.setSize(0, 0);
		try {
			loadImage(tranColor, imgFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ImageActor(Image... img) {
		super();
		this.img = img;
		if (img != null && img[0] != null) {
			setWidth(this.img[0].getWidth());
			setHeight(this.img[0].getHeight());
		}
	}

	/**
	 * @param imageFile
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param speed_x
	 * @param speed_y
	 * @throws SlickException
	 */
	public ImageActor(float x, float y, float speed_x, float speed_y,
			String... imageFile) throws IOException {
		super(x, y, 20, 20, speed_x, speed_y);
		this.imgFile = imageFile;
		this.setSize(0, 0);
		try {
			loadImage(tranColor, imgFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ImageActor(float x, float y, float speed_x, float speed_y,
			Image... img) {
		super(x, y, 20, 20, speed_x, speed_y);
		this.img = img;
		if (img != null && img[0] != null) {
			this.setSize(img[0].getWidth(), img[0].getHeight());
		}
	}

	protected void loadImage(Color tranColor, String... imageFile)
			throws IOException {
		try {
			this.img = new Image[imageFile.length];
			this.imgFile = imageFile;
			this.tranColor = tranColor;
			for (int i = 0; i < imageFile.length; i++) {
				this.img[i] = new Image(imageFile[i], tranColor);
			}
			curframe = 0;
			if (img[0] != null) {
				if (getWidth() == 0)
					setWidth(img[0].getWidth());
				if (getHeight() == 0)
					setHeight(img[0].getHeight());
			}
		} catch (SlickException e) {
			throw new IOException(e.getMessage());
		}
	}

	public void paint(Graphics g) {
		if (isDead())
			return;

		if (img == null && imgFile != null) {
			try {
				loadImage(tranColor, imgFile);
			} catch (IOException e) {
				e.printStackTrace();
				img = null;
			}
		}
		if (img[curframe] == null && imgFile != null) {
			if (getWidth() <= 0)
				setWidth(20);
			if (getHeight() <= 0)
				setHeight(20);
			super.paint(g);
			g.setColor(new Color(255, 255, 0, 120));
			g.drawString(imgFile[curframe], 2, 10);
			g.drawString("not found", 2, 20);
		} else {
			if (img[curframe] != null) {
				img[curframe].draw(0, 0, getWidth(), getHeight(), getFilter());
			}
		}

	}

	public Image getImg() {
		return img[curframe];
	}

	public void setImg(Image... img) {
		this.img = img;
		if (img != null&& img[0]!=null) {
			this.setSize(img[0].getWidth(), img[0].getHeight());
		}
		curframe = 0;
	}

	public String getImgFile() {
		return imgFile[0];
	}

	public void setImgFile(String... imgFile) {
		if (imgFile.equals(imgFile)) {
			this.imgFile = imgFile;
			try {
				loadImage(tranColor, imgFile);
			} catch (IOException e) {
			}
		}
	}

	public Color getTranColor() {
		return tranColor;
	}

	public void setTranColor(Color tranColor) {
		if (tranColor != null) {
			this.tranColor = tranColor;
			try {
				loadImage(tranColor, imgFile);
			} catch (IOException e) {
			}
		}
		this.tranColor = tranColor;
	}

	public boolean contains(float x2, float y2) {
		boolean t = super.contains(x2, y2);
		if (checkPixel) {
			if (!t || img == null)
				return false;
			int cx = (int) ((x2 - getX()) / getScalex());
			int cy = (int) ((y2 - getY()) / getScaley());
			Color c = getPixel(cx, cy);
			return c.a > 0f;
		}
		return t;
	}

	static Color blank = new Color(0, 0, 0, 0);

	public Color getPixel(int x, int y) {
		if (x < 0 || x >= getWidth())
			return blank;
		if (y < 0 || y >= getHeight())
			return blank;
		return img[curframe].getColor(x, y);
	}

	public boolean isCheckPixel() {
		return checkPixel;
	}

	public void setCheckPixel(boolean checkPixel) {
		this.checkPixel = checkPixel;
	}

	public int getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}

	public int getCurframe() {
		return curframe;
	}

	public void setCurframe(int curframe) {
		if (img == null)
			return;
		this.curframe = curframe;
		if (this.curframe >= img.length)
			this.curframe = img.length;
		if (this.curframe <= 0)
			this.curframe = 0;
	}

	public boolean isActive() {
		if (img == null || img.length <= 1)
			return false;
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	private int ntime = 0;

	@Override
	public void update(int time) {
		super.update(time);
		if (isActive()) {
			ntime += time;
			while (ntime > delay) {
				ntime -= delay;
				curframe++;
				if (curframe >= img.length)
					curframe = 0;
			}
		}
	}
}
